﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class BienLaiPhatDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Bienlaiphat");
        }

        public DataTable Select_BienLaiPhat(object obj)
        {
            var o = (BienLaiPhat)obj;
            var sql = "set dateformat dmy; execute sp_InBienlaiphat_Sel '{0}' ";
            sql = string.Format(sql, o.STT_BLP);
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Bienlaiphat {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (BienLaiPhat)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Bienlaiphat '{0}', N'{1}', '{2}', '{3}', '{4}', '{5}' ";
            sql = string.Format(sql, o.STT_BLP, o.LYDOPHAT, o.SOTIENPHAT, o.NGAYPHAT.ToString("dd/MM/yyyy"), o.MACUONSACH, o.STT_PM);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (BienLaiPhat)obj;
            var sql = "set dateformat dmy; execute sp_Update_Bienlaiphat '{0}', N'{1}', '{2}', '{3}', '{4}', '{5}' ";
            sql = string.Format(sql, o.STT_BLP, o.LYDOPHAT, o.SOTIENPHAT, o.NGAYPHAT, o.MACUONSACH, o.STT_PM);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select * from tb_Bienlaiphat";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.BienLaiPhat o)
        {
            string sql;
            if (o.STT_BLP != "")
                sql = @"Select * from tb_Bienlaiphat Where STT_BLP like '" + o.STT_BLP + "' ";
            else 
                sql = @"Select * from tb_Bienlaiphat Where MACUONSACH like '" + o.MACUONSACH + "' or STT_PM like '" + o.STT_PM + "' ";
           
            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(STT_BLP) as Ma From tb_Bienlaiphat";
            return ExecuteQuery(sql);
        }


        #endregion
    }
}
